package top.marken.onlineclass.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.marken.onlineclass.dao.EpisodeMapper;
import top.marken.onlineclass.dao.PlayRecordMapper;
import top.marken.onlineclass.dao.VideoMapper;
import top.marken.onlineclass.dao.VideoOrderMapper;
import top.marken.onlineclass.exception.ProjectException;
import top.marken.onlineclass.model.entity.Episode;
import top.marken.onlineclass.model.entity.PlayRecord;
import top.marken.onlineclass.model.entity.Video;
import top.marken.onlineclass.model.entity.VideoOrder;
import top.marken.onlineclass.service.VideoOrderService;
import top.marken.onlineclass.util.JsonData;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VideoOrderServiceImpl implements VideoOrderService {
    @Autowired
    private VideoOrderMapper videoOrderMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private EpisodeMapper episodeMapper;
    @Autowired
    private PlayRecordMapper playRecordMapper;

    @Override
    public VideoOrder findVideoOrder(int videoId, int userId, int state) {
        return videoOrderMapper.findByVideoIdAndUserIdAndState(videoId,userId,state);
    }

    @Override
    @Transactional
    public int save(int videoId, int userId, int state) {
        VideoOrder videoOrder = videoOrderMapper.findByVideoIdAndUserIdAndState(videoId,userId,VideoOrder.STATE_PAID);
        if (videoOrder != null) {
            return SAVE_CODE_ALREADY_EXIST;
        }
        try {
            Video video = videoMapper.findById(videoId);
            if (video == null) {
                return SAVE_CODE_FAIL;
            }
            VideoOrder newVideoOrder = new VideoOrder();
            newVideoOrder.setVideoId(videoId);
            newVideoOrder.setVideoImg(video.getCoverImg());
            newVideoOrder.setVideoTitle(video.getTitle());
            newVideoOrder.setCreateTime(new Date());
            newVideoOrder.setUserId(userId);
            newVideoOrder.setState(state);
            newVideoOrder.setOutTradeNo(UUID.randomUUID().toString());
            newVideoOrder.setTotalFee(video.getPrice());
            if (videoOrderMapper.insertOne(newVideoOrder) == 1) {
                // 默认向播放记录插入一条播放第一条视频的记录
                Episode episode = episodeMapper.findFirstByVideoId(videoId);
                if (episode == null) {
                    return SAVE_CODE_SUCCESS;
                }
                PlayRecord playRecord = new PlayRecord();
                playRecord.setVideoId(videoId);
                playRecord.setUserId(userId);
                playRecord.setCurrentNum(episode.getNum());
                playRecord.setEpisodeId(episode.getId());
                playRecord.setCreateTime(new Date());
                playRecordMapper.insertOne(playRecord);
                return SAVE_CODE_SUCCESS;
            } else {
                return SAVE_CODE_FAIL;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return SAVE_CODE_ERROR;
        }

    }

    @Override
    public List<VideoOrder> listVideoOrderByUserId(int userId, int state) {
        return videoOrderMapper.findByUserId(userId, state);
    }
}
